var strophe = require('../../utils/strophe.js')
var WebIM = require('../../utils/WebIM.js')
var WebIM = WebIM.default
var app = getApp();
var bianli;
var msgArr;
var personArr;
var show;

Page({
  data:{
    bianli:true,
    chatMsg:[],
    
  },
  
  onShow:function(){
    var that = this;
  //  console.log(personArr)
   
  //  for(var i=0;i<personArr.length;i++){
  //    if (!personArr[i].id2&&personArr[i].isShow) {
  //      that.setData({
  //        showView: true
  //      })
    
  //    }else{
  //      that.setData({
  //        showView: false
  //      })
  //    }
  //  }
    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let wh = res.windowHeight;
        let imgWidth = ww * 0.48;
        let scrollH = wh;

        that.setData({
          scrollH: scrollH,
          imgWidth: imgWidth,
          w: ww,
          wh: wh
        });

      }
    })
  
  
   personArr = wx.getStorageSync('personArr')||[];
   //console.log('personArr',personArr)
   for(var i=0;i<personArr.length;i++){
     if (personArr[i].isShow){
       var id = i
       that.setData({
         isShow:true,
         id:id
       })
     }
   
     var time = personArr[i].MsgArr[0].time
     var date = new Date(time)
     var day = date.getHours() + ":" + date.getMinutes()
     personArr[i].MsgArr[0].day = day
     //console.log(day)
   }
   
   that.setData({
     personArr: personArr
   })
  // console.log(personArr)

   if (personArr == '') {
     that.setData({
       show: true
     })
   } else {
     that.setData({
       show: false
     })
   }
   
   try {
     wx.setStorageSync('personArr', personArr)
   } catch (e) {
   }
    

  //     var tmp = chatMsg2

  //     var result1 = [],hash = {}, hash_ = {}
  //     for (var i = tmp.length - 1; i >= 0; i--) {
  //       var elem = tmp[i].yourname;
  //       var elem_ = tmp[i].time;
  //       if (!hash[elem]) {
  //         if (!hash_[elem_]) {
  //           result1.push(tmp[i]);
  //           hash_[elem_] = true;
  //         };
  //         hash[elem] = true;
  //       }

  //     }
    
  //   console.log(result1)


  },
  onLoad:function(){
    var that = this;
  
    
  },

  //遮罩层
  openOpcaty: function () {
    var that = this;
    that.setData({
      isOpacity: true
    })
  },
  colse: function () {
    var that = this;
    that.setData({
      isOpacity: false
    })
  },

  //进入发布动态
  pubstate: function () {
    var that = this;
    wx.navigateTo({
      url: '../dynamics/dynamics'
    })
    that.setData({
      isOpacity: false
    })
  },
  //进入发拼座
  openInter: function () {
    var that = this;
    wx.navigateTo({
      url: '../interaction/interaction'
    })
    that.setData({
      isOpacity: false
    })
  },

  into_chat:function(e){
    var that = this
    //console.log(e)
    var access_token = wx.getStorageSync('inToken');
    var uid = wx.getStorageSync('uid');
    var myName = wx.getStorageSync('myUsername')
    var evt = e.currentTarget.dataset
    personArr = wx.getStorageSync('personArr')||[];
    //console.log(personArr)
    var idx = e.currentTarget.dataset.idx
    for (var i = 0; i < personArr.length; i++) {
       if(idx==i){
         personArr[i].isShow = false
         that.setData({
           isShow:false
         })
       }
       try {
         wx.setStorageSync('personArr', personArr)
       } catch (e) {
       }
    }
    

    var nameList = {
      myName: myName,
      your: evt.yuid,
      yourName: evt.yname,
      userAvatar: evt.pic
    }

    wx.navigateTo({
      url: '../chat/chat?username=' + JSON.stringify(nameList)
    })
  },

  //提示开发中
  develop: function () {
    wx.showModal({
      title: '提示',
      content: '功能正在开发中，敬请期待',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {

        }
      }
    })
  },
  //返回上页
  backTag: function () {
    wx.navigateBack({
      delta: 2
    })
  },
  //跳转另个小程序
  tiaozhuan: function () {
    wx.navigateToMiniProgram({
      appId: 'wx3abd6a1e615ce065',
      path: '',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  }
  

})